//
//  IMMessageLoadMoreTableViewCell.m
//  MLIM
//
//  Created by molon on 15/7/13.
//  Copyright (c) 2015年 molon. All rights reserved.
//

#import "IMMessageLoadMoreTableViewCell.h"
#import <MLKit.h>

@interface IMMessageLoadMoreTableViewCell()

@property (nonatomic, strong) UIActivityIndicatorView *indicatorView;
@property (nonatomic, strong) UIButton *retryButton;

@end

@implementation IMMessageLoadMoreTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.clipsToBounds = YES;
        
        [self.contentView addSubview:self.indicatorView];
        [self.contentView addSubview:self.retryButton];
    }
    return self;
}

- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - getter
- (UIActivityIndicatorView *)indicatorView
{
    if (!_indicatorView) {
        _indicatorView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    }
    return _indicatorView;
}

- (UIButton *)retryButton
{
    if (!_retryButton) {
        UIButton *button = [[UIButton alloc]init];
//        button.backgroundColor = [UIColor colorWithWhite:0.752 alpha:1.000];
//        [button setTitle:@"点击加载旧消息" forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont boldSystemFontOfSize:14.0f];
        [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [button setImage:[[UIImage imageNamed:MLKIT_BUNDLE_FILE(@"刷新")]imageWithTintColor:[UIColor grayColor]] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(retryButtonEvent) forControlEvents:UIControlEventTouchUpInside];
        
        _retryButton = button;
    }
    return _retryButton;
}

#pragma mark - event
- (void)retryButtonEvent
{
    if (self.clickRetryButtonBlock) {
        self.clickRetryButtonBlock(YES);
    }
}

#pragma mark - layout
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.indicatorView.center = CGPointMake(self.contentView.frameWidth/2, self.contentView.frameHeight/2);
    self.retryButton.frame = self.contentView.bounds;
}

#pragma mark - outcall
- (void)animatingLoadMoreCell:(BOOL)animate
{
    self.retryButton.hidden = animate;
    animate?[self.indicatorView startAnimating]:[self.indicatorView stopAnimating];
}

#pragma mark - height
+ (CGFloat)heightForObject:(id)object maxWidth:(CGFloat)maxWidth
{
    return 60.0f;
}

@end
